package ${packageName}.rpc.provide.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.educate.common.utils.converter.ListUtils;
import com.educate.common.utils.exception.AppException;
import com.educate.common.web.BeanUtils;
import ${packageName}.dao.${ModelName}Dao;
import ${packageName}.model.bo.${ModelName}BO;
import ${packageName}.model.po.${ModelName};
import ${packageName}.rpc.api.${ModelName}Service;
import lombok.extern.log4j.Log4j2;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

@Log4j2
@Service(
        version = "${r'${'}api.service.version}",
        application = "${r'${'}dubbo.application.id}",
        protocol = "${r'${'}dubbo.protocol.id}",
        registry = "${r'${'}dubbo.registry.id}"
)
public class ${ModelName}ServiceImpl implements ${ModelName}Service {

    @Resource ${ModelName}Dao ${modelName}Dao;

    @Override
    public void create(${ModelName}BO bo) {
        ${modelName}Dao.insert(bo);
    }

    @Override
    public void delete(${ModelName}BO bo) {
        ${modelName}Dao.delete(bo);
    }

    @Override
    public void delete(String ${modelName}Id) {
        ${modelName}Dao.deleteById(${modelName}Id);
    }

    @Override
    public ${ModelName}BO detail(String ${modelName}Id) {
        Optional<${ModelName}> optional = ${modelName}Dao.findById(${modelName}Id);
        if (!optional.isPresent()) {
            throw new AppException("不存在的实例");
        }
        return new ${ModelName}BO(optional.get());
    }

    @Override
    public ${ModelName}BO detail(${ModelName}BO bo) {
//        return detail(bo.getOrganizationId());
        return null;
    }

    @Override
    public void update(${ModelName}BO bo) {
        Optional<${ModelName}> optional = ${modelName}Dao.findById(bo.get${ModelName}Id());
        if (!optional.isPresent()) {
            throw new AppException("不存在的实例");
        }
        BeanUtils.copyPropertiesNonEmpty(bo, optional.get());
        ${modelName}Dao.save(optional.get());
    }

    @Override
    public Page<${ModelName}BO> query(${ModelName}BO bo, PageRequest pageRequest) {
        return ${modelName}Dao.query(bo, pageRequest).map(${ModelName}BO::new);
    }

    @Override
    public List<${ModelName}BO> list(${ModelName}BO bo) {
        List<${ModelName}> result = ${modelName}Dao.findAll();
        return ListUtils.map(result, ${ModelName}BO::new);
    }
}
